//
//  NSDate+Anna.m
//  ShenWeiRent
//
//  Created by iMac on 2019/3/26.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "NSDate+Anna.h"
#import "NSCalendar+DateManipulation.h"

@implementation NSDate (Anna)

- (NSInteger)numberOfMonthFromStartDate:(NSDate *)startDate
{
    NSDate *endDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSInteger endYear = [calendar component:NSCalendarUnitYear fromDate:endDate];
    NSInteger endMonth = [calendar component:NSCalendarUnitMonth fromDate:endDate];
    NSInteger startYear = [calendar component:NSCalendarUnitYear fromDate:startDate];
    NSInteger startMonth = [calendar component:NSCalendarUnitMonth fromDate:startDate];
    NSInteger months = (endYear - startYear) * 12 - (startMonth - 1) + endMonth;
    return months;
}

- (NSArray *)todayBetweenEndDateWithMonths:(int)months
{
    NSMutableArray *temp = @[].mutableCopy;
    NSDate *endDate = [[NSCalendar currentCalendar] dateByAddingMonths:months toDate:self];
    NSInteger startYear = [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:self];
    NSInteger startMonth = [[NSCalendar currentCalendar] component:NSCalendarUnitMonth fromDate:self];
    NSInteger startDay = [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:self];
    NSInteger endYear = [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:endDate];
    NSInteger endMonth = [[NSCalendar currentCalendar] component:NSCalendarUnitMonth fromDate:endDate];
    NSInteger endtDay = [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:endDate];
    NSDateComponents *com = [[NSDateComponents alloc] init];
    // 同一年
    if (endYear == startYear) {
        NSInteger monthCount = endMonth - startMonth + 1;
        NSMutableArray *monthArray = @[].mutableCopy;
        for (int i = 0; i < monthCount; i++) {
            com.year = endYear;
            com.month = startMonth + i;
            com.day = 1;
            NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:com];
            NSInteger days = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date].length;
            NSMutableArray *dayArray = @[].mutableCopy;
            if (i == 0) {
                for (NSInteger j = startDay+1; j <= days; j++) {
                    com.day = j;
                    NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:com];
                    [dayArray addObject:date];
                }
            }
            else {
                for (NSInteger j = 1; j <= days; j++) {
                    com.day = j;
                    NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:com];
                    [dayArray addObject:date];
                }
            }
            [monthArray addObject:dayArray.copy];
        }
        [temp addObject:monthArray];
    }
    else {// 不是同一年
        // 计算第一年
        NSMutableArray *firstMonthArray = @[].mutableCopy;
        for (int i = 0; i < 12-startMonth+1; i++) {
            com.year = startYear;
            com.month = startMonth + i;
            com.day = 1;
            NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:com];
            NSInteger days = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date].length;
            NSMutableArray *dayArray = @[].mutableCopy;
            if (i == 0) {
                for (NSInteger j = startDay+1; j <= days; j++) {
                    com.day = j;
                    NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:com];
                    [dayArray addObject:date];
                }
            }
            else {
                for (int j = 1; j <= days; j++) {
                    com.day = j;
                    NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:com];
                    [dayArray addObject:date];
                }
            }
            [firstMonthArray addObject:dayArray.copy];
        }
        [temp addObject:firstMonthArray];
        // 计算中间的整年
        for (int i = 0; i < endYear - startYear - 1; i++) {
            NSMutableArray *monthArray = @[].mutableCopy;
            for (int j = 1; j <= 12; j++) {
                com.year = startYear + i + 1;
                com.month = j;
                com.day = 1;
                NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:com];
                NSInteger days = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date].length;
                NSMutableArray *dayArray = @[].mutableCopy;
                for (int k = 1; k <= days; k++) {
                    com.day = k;
                    NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:com];
                    [dayArray addObject:date];
                }
                [monthArray addObject:dayArray.copy];
            }
            [temp addObject:monthArray];
        }
        // 计算最后一年
        NSMutableArray *lastMonthArray = @[].mutableCopy;
        for (int i = 0; i < endMonth; i++) {
            com.year = endYear;
            com.month = i + 1;
            com.day = 1;
            NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:com];
            NSInteger days = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date].length;
            NSMutableArray *dayArray = @[].mutableCopy;
            for (int k = 1; k <= days; k++) {
                com.day = k;
                NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:com];
                [dayArray addObject:date];
            }
            [lastMonthArray addObject:dayArray.copy];
        }
        [temp addObject:lastMonthArray];
    }
    return temp.copy;
}

- (NSArray *)monthPickerDataSourceFromStartDate:(NSDate *)startDate
{
    NSMutableArray *temp = @[].mutableCopy;
    
    NSDate *endDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSInteger endYear = [calendar component:NSCalendarUnitYear fromDate:endDate];
    NSInteger endMonth = [calendar component:NSCalendarUnitMonth fromDate:endDate];
    NSInteger startYear = [calendar component:NSCalendarUnitYear fromDate:startDate];
    NSInteger startMonth = [calendar component:NSCalendarUnitMonth fromDate:startDate];
    
    NSDateComponents *com = [[NSDateComponents alloc] init];
    // 同一年
    if (endYear == startYear) {
        NSInteger monthCount = endMonth - startMonth + 1;
        NSMutableArray *monthArray = @[].mutableCopy;
        for (int i = 0; i < monthCount; i++) {
            com.year = endYear;
            com.month = startMonth + i;
            com.day = 1;
            NSDate *date = [calendar dateFromComponents:com];
            [monthArray addObject:date];
        }
        [temp addObject:monthArray];
    }
    else {// 不是同一年
        // 计算第一年
        NSMutableArray *firstMonthArray = @[].mutableCopy;
        for (int i = 0; i < 12-startMonth+1; i++) {
            com.year = startYear;
            com.month = startMonth + i;
            com.day = 1;
            NSDate *date = [calendar dateFromComponents:com];
            [firstMonthArray addObject:date];
        }
        [temp addObject:firstMonthArray];
        // 计算中间的整年
        for (int i = 0; i < endYear - startYear - 1; i++) {
            NSMutableArray *monthArray = @[].mutableCopy;
            for (int j = 1; j <= 12; j++) {
                com.year = startYear + i + 1;
                com.month = j;
                com.day = 1;
                NSDate *date = [calendar dateFromComponents:com];
                [monthArray addObject:date];
            }
            [temp addObject:monthArray];
        }
        // 计算最后一年
        NSMutableArray *lastMonthArray = @[].mutableCopy;
        for (int i = 0; i < endMonth; i++) {
            com.year = endYear;
            com.month = i + 1;
            com.day = 1;
            NSDate *date = [calendar dateFromComponents:com];
            [lastMonthArray addObject:date];
        }
        [temp addObject:lastMonthArray];
    }
    return temp.copy;
}

@end
